package com.qf.service.impl;

import com.qf.entity.Business;
import com.qf.entity.Task;
import com.qf.enums.OAExceptionEnum;
import com.qf.exception.OAException;
import com.qf.mapper.BusinessMapper;
import com.qf.mapper.TaskMapper;
import com.qf.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author 程云
 * @Date 2019/12/11 10:30
 * 人生在世 代码二字
 */
@Service
@Slf4j
public class BusinessServiceImpl implements BusinessService {

    @Resource
    private BusinessMapper businessMapper;
    @Resource
    private TaskMapper taskMapper;

    @Override
    @Transactional
    public Integer add(Business business) {
        //初始化审核状态，默认待审核
        business.setAuditStatus(0);
        business.setAuditTime(new Date());
        //任务类型，出差
        business.setTaskId(2);
        Integer count = businessMapper.insertInfo(business);
        if (count != 1){
            log.info("【出差事件】 申请失败 count = {}",count);
            throw new OAException(OAExceptionEnum.SHENGQING_SHIBAII.getCode(),OAExceptionEnum.SHENGQING_SHIBAII.getMsg());
        }
        Task param = new Task();
        param.setApplyName(business.getRealName());
        param.setApplyTime(business.getAuditTime());
        param.setTaskId(business.getTaskId());
        param.setStatus(business.getAuditStatus());
        param.setUserId(business.getUserId());
        param.setEventId(business.getBusinessId());
        int num = taskMapper.insert(param);
        return count;
    }
}
